package com.youlu.campus.web.community.service.impl;

import com.alibaba.fastjson.JSON;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.base.base.ContentTypeEnum;
import com.youlu.campus.base.base.InteractiveActionEnum;
import com.youlu.campus.base.base.PageQueryRequest;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.service.base.BaseService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.qq.QQService;
import com.youlu.campus.web.certificate.entity.CertificatePO;
import com.youlu.campus.web.community.dto.*;
import com.youlu.campus.web.community.entity.*;
import com.youlu.campus.web.community.service.InteractiveService;
import com.youlu.campus.service.integral.UserIntegralService;
import com.youlu.campus.entity.UserPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 动态互动相关接口实现类
 * 评论、点赞、分享
 *
 * @author: YangJ
 * @date: 2020/11/26 20:30
 */
@Slf4j
@Service("interactiveService")
public class InteractiveServiceImpl extends BaseService implements InteractiveService {

    @Autowired
    private QQService qqService;

//    @Autowired
//    private MQService mqService;

    @Autowired
    private UserIntegralService userIntegralService;

    @Override
    public boolean firstComment(CommentFirstRequest request) {

        //检查内容安全
        if (qqService.hasIllegal(request.getCommentContent())) {
            throw new BusinessException("内容违规请重新发布!");
        }

        UserPO user = RequestContext.getCurUser();
        ContentComment comment = mapperFacade.map(request, ContentComment.class);
        comment.setCommentatorId(user.getId());
        comment.setCommentatorAvatarUrl(user.getAvatarUrl());
        comment.setCommentatorNickName(user.getNickName());
        comment.setLevel(1);
        //自我评论
        if (request.getContentLinkId().equals(user.getId())) {
            comment.setType(2);
        }
        mongoTemplate.insert(comment);

        //给他人评论+积分
        //维护互动信息-评论数
        InteractiveMqRequest req = new InteractiveMqRequest();
        req.setType(comment.getContentType());
        req.setAction(InteractiveActionEnum.COMMENT.getCode());
        req.setBid(comment.getContentId());
        req.setValue(1);
//        mqService.sendNormal(req, CampusMQDefinition.ROUTING_KEY_INTERACTIVE);
        return true;
    }

    @Override
    public boolean secondComment(CommentSecondRequest request) {

        //检查内容安全
        if (qqService.hasIllegal(request.getCommentContent())) {
            throw new BusinessException("内容违规请重新发布!");
        }

        ContentComment firstComment = findById(request.getCommentId());
        if (Objects.isNull(firstComment)) {
            log.error(":>>> 评论信息不存在,ID:{}", request.getCommentId());
            throw new BusinessException("评论信息错误!");
        }
        UserPO user = RequestContext.getCurUser();
        ContentComment comment = mapperFacade.map(request, ContentComment.class);
        comment.setCommentatorId(user.getId());
        comment.setCommentatorAvatarUrl(user.getAvatarUrl());
        comment.setCommentatorNickName(user.getNickName());
        if (firstComment.getContentLinkId().equals(user.getId())) {
            comment.setType(2);
        }
        comment.setLevel(2);

        mongoTemplate.insert(comment);

        //给他人评论+积分
//        if(comment.getType().equals(1)){
//            userIntegralService.addIntegral(user.getId(),
//                    UserIntegralLogBusinessTypeEnum.COMMENT.getIntegral(), UserIntegralLogBusinessTypeEnum.COMMENT.getValue());
//        }
        //维护互动信息-评论数
        InteractiveMqRequest req = new InteractiveMqRequest();
        req.setType(firstComment.getContentType());
        req.setAction(InteractiveActionEnum.COMMENT.getCode());
        req.setBid(firstComment.getContentId());
        req.setValue(1);
//        mqService.sendNormal(req, CampusMQDefinition.ROUTING_KEY_INTERACTIVE);

        return true;
    }

    private ContentComment findById(String commentId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(commentId));
        ContentComment comment = mongoTemplate.findOne(query, ContentComment.class);
        return comment;
    }

    @Override
    public Page<CommentResponse> commentList(CommentQueryRequest request) {
        log.info(":>>> 开始查询评论:{}", JSON.toJSONString(request));
        Query query = new Query();

        query.addCriteria(Criteria.where("show").is(true));
//        if (StringUtils.isNotBlank(request.getContentType())) {
//            query.addCriteria(Criteria.where("contentType").is(request.getContentType()));
//        }
        if (StringUtils.isNotBlank(request.getContentId())) {
            query.addCriteria(Criteria.where("contentId").is(request.getContentId()));
        }
        if (Objects.nonNull(request.getLevel())) {
            query.addCriteria(Criteria.where("level").is(request.getLevel()));
        }
        if (StringUtils.isNotBlank(request.getCommentId())) {
            log.warn(":>>> 评论ID不为空");
            query.addCriteria(Criteria.where("commentId").is(request.getCommentId()));
        }
        long count = mongoTemplate.count(query, ContentComment.class);

        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "type"));
//        query.with(new Sort(Sort.Direction.DESC, "likeCount"));
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));

        List<ContentComment> commentList = mongoTemplate.find(query.with(pageRequest), ContentComment.class);
        List<CommentResponse> list = commentList.stream().map(comment -> {
            CommentResponse response = mapperFacade.map(comment, CommentResponse.class);
            response.setLiked(queryIsLike(comment.getId(), ContentTypeEnum.PL.getCode()));
            if (comment.getLevel().equals(1)) {
                response.setSecondList(querySecondList(comment.getId()));
            }
            return response;
        }).collect(Collectors.toList());
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    /**
     * 查询二级评论信息
     * 第一条二级评论+二级评论总条数
     *
     * @param commentId
     * @return
     */
    private Map<String, Object> querySecondList(String commentId) {

        Query query = new Query();
        query.addCriteria(Criteria.where("show").is(true));
        query.addCriteria(Criteria.where("commentId").is(commentId));
        long count = mongoTemplate.count(query, ContentComment.class);
        if (count <= 0) {
            return null;
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("count", count);
//        query.with(new Sort(Sort.Direction.DESC, "type"));
//        query.with(new Sort(Sort.Direction.DESC, "likeCount"));
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        ContentComment comment = mongoTemplate.findOne(query, ContentComment.class);
        CommentResponse response = mapperFacade.map(comment, CommentResponse.class);
        response.setLiked(queryIsLike(comment.getId(), ContentTypeEnum.PL.getCode()));
        map.put("comment", response);

        return map;
    }


    @Override
    public boolean like(ContentLikeRequest request) {

        UserPO user = RequestContext.getCurUser();

        boolean b = queryIsLike(request.getInfoId(), request.getType());
        if(b){
            log.info("已点赞，不可重复点赞！,infoId:{},userId:{}",request.getInfoId(),user.getId());
            return false;
        }
        ContentLike like = mapperFacade.map(request, ContentLike.class);
        like.setCreatorId(user.getId());
        like.setCreatorNickName(user.getNickName());
        like.setCreatorAvatar(user.getAvatarUrl());
        mongoTemplate.insert(like);

        //给他人点赞+积分
//        if(!user.getId().equals(request.getInfoCreatorId())){
//            userIntegralService.addIntegral(user.getId(),
//                    UserIntegralLogBusinessTypeEnum.LIKE.getIntegral(), UserIntegralLogBusinessTypeEnum.LIKE.getValue());
//        }
        //维护互动信息-点赞数+1
        likeCount(like.getType(),like.getInfoId(),1);

        return true;
    }

    private void likeCount(String type,String id,Integer value){
        if(ContentTypeEnum.PL.getCode().equals(type)){
            //维护关联内容互动信息-点赞数
            ContentComment comment = findById(id);
            if(comment != null){
                if (comment.getLevel().equals(2)){
                    Query query1 = new Query();
                    query1.addCriteria(Criteria.where("id").is(comment.getCommentId()));
                    comment = mongoTemplate.findOne(query1, ContentComment.class);
                }
                //维护互动信息-点赞数
                InteractiveMqRequest req = new InteractiveMqRequest();
                req.setType(comment.getContentType());
                req.setAction(InteractiveActionEnum.LIKE.getCode());
                req.setBid(comment.getContentId());
                req.setValue(value);
//                mqService.sendNormal(req, CampusMQDefinition.ROUTING_KEY_INTERACTIVE);
            }
        }
        //维护互动信息-点赞数
        InteractiveMqRequest req = new InteractiveMqRequest();
        req.setType(type);
        req.setAction(InteractiveActionEnum.LIKE.getCode());
        req.setBid(id);
        req.setValue(value);
//        mqService.sendNormal(req, CampusMQDefinition.ROUTING_KEY_INTERACTIVE);

    }

    @Override
    public boolean unlike(ContentLikeRequest request) {

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("infoId").is(request.getInfoId()));
        query.addCriteria(Criteria.where("creatorId").is(user.getId()));
        DeleteResult result = mongoTemplate.remove(query, ContentLike.class);
        if (result.getDeletedCount() > 0) {
            log.info("取消点赞成功,infoId:{},creatorId:{}", request.getInfoId(), user.getId());

            //维护互动信息-点赞数-1
            likeCount(request.getType(),request.getInfoId(),-1);
        }
        return true;
    }

    @Override
    public boolean queryIsLike(String infoId, String type) {
        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("infoId").is(infoId));
        query.addCriteria(Criteria.where("type").is(type));
        query.addCriteria(Criteria.where("creatorId").is(user.getId()));
        return mongoTemplate.exists(query, ContentLike.class);
    }

    @Override
    public List<String> queryLikeUser(String infoId, String type) {

        Query query = new Query();
        query.addCriteria(Criteria.where("infoId").is(infoId));
        query.addCriteria(Criteria.where("type").is(type));
        PageRequest pageRequest = PageRequest.of(0, 5);
        List<ContentLike> likes = mongoTemplate.find(query.with(pageRequest), ContentLike.class);
        if (likes != null && likes.size() > 0) {
            List<String> list = new ArrayList<>();
            for (ContentLike like : likes) {
                list.add(like.getCreatorAvatar());
            }
            return list;
        }

        return null;
    }

    @Override
    public boolean share(ContentShareRequest request) {

        UserPO user = RequestContext.getCurUser();
        ContentShare share = mapperFacade.map(request, ContentShare.class);
        share.setCreatorId(user.getId());
        mongoTemplate.insert(share);

        //分享好友+积分
//        userIntegralService.addIntegral(user.getId(),
//                UserIntegralLogBusinessTypeEnum.SHARE.getIntegral(), UserIntegralLogBusinessTypeEnum.SHARE.getValue());

        //维护互动信息-分享数
        InteractiveMqRequest req = new InteractiveMqRequest();
        req.setType(share.getType());
        req.setAction(InteractiveActionEnum.SHARE.getCode());
        req.setBid(share.getInfoId());
        req.setValue(1);
//        mqService.sendNormal(req, CampusMQDefinition.ROUTING_KEY_INTERACTIVE);

        return true;
    }

    @Override
    public boolean followUser(FollowUserRequest request) {

        UserPO user = RequestContext.getCurUser();

        UserFollow follow = mapperFacade.map(request, UserFollow.class);
        follow.setFollowerId(user.getId());
        follow.setFollowerNickName(user.getNickName());
        follow.setFollowerAvatarUrl(user.getAvatarUrl());
        //判断是否已关注我
        UserFollow userFollow = queryFollow(user.getId(), request.getUserId());
        if (userFollow == null) {
            //未关注时-设定为已关注状态
            follow.setStatus(1);
        } else {
            //已关注我时-设定为相互关注状态
            follow.setStatus(2);
            //更新他关注我的信息，为相互关注
            userFollow.setStatus(2);
            mongoTemplate.save(userFollow);
        }
        mongoTemplate.insert(follow);

        return true;
    }

    @Override
    public boolean queryIsFollow(String userId) {
        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.addCriteria(Criteria.where("followerId").is(user.getId()));
        return mongoTemplate.exists(query, UserFollow.class);
    }

    @Override
    public boolean unFollowUser(String userId) {
        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.addCriteria(Criteria.where("followerId").is(user.getId()));
        DeleteResult result = mongoTemplate.remove(query, UserFollow.class);
        if (result.getDeletedCount() > 0) {
            //恢复相互关注为 已关注
            UserFollow userFollow = queryFollow(user.getId(), userId);
            if (userFollow != null && userFollow.getStatus().equals(2)) {
                userFollow.setStatus(1);
                mongoTemplate.save(userFollow);
            }
        }

        return true;
    }

    private UserFollow queryFollow(String userId, String followerId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.addCriteria(Criteria.where("followerId").is(followerId));
        UserFollow follow = mongoTemplate.findOne(query, UserFollow.class);
        return follow;
    }

    @Override
    public Page<FollowResponse> followList(FollowQueryRequest request) {

        String userId;
        if (StringUtils.isNotBlank(request.getUserId())) {
            userId = request.getUserId();
        } else {
            UserPO user = RequestContext.getCurUser();
            userId = user.getId();
        }
        Query query = new Query();
        if ("1".equals(request.getType())) {
            query.addCriteria(Criteria.where("userId").is(userId));
        } else {
            query.addCriteria(Criteria.where("followerId").is(userId));
        }
        long count = mongoTemplate.count(query, UserFollow.class);

        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "status"));
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));

        List<UserFollow> userFollows = mongoTemplate.find(query.with(pageRequest), UserFollow.class);
        List<FollowResponse> list = userFollows.stream().map(userFollow -> {
            FollowResponse response = mapperFacade.map(userFollow, FollowResponse.class);
            if("1".equals(request.getType()) && userFollow.getStatus().equals(1)){
                //粉丝列表状态返回 0 已关注我
                response.setStatus(0);
            }
            return response;
        }).collect(Collectors.toList());

        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    @Override
    public InteractiveResponse userInteractive(String userId) {

        String id;
        if (StringUtils.isBlank(userId)) {
            UserPO user = RequestContext.getCurUser();
            id = user.getId();
        } else {
            id = userId;
        }

        InteractiveResponse response = new InteractiveResponse();

        Query query = new Query();
        query.addCriteria(Criteria.where("followerId").is(id));
        long followCount = mongoTemplate.count(query, UserFollow.class);
        response.setFollowCount(followCount);
        query = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        long fansCount = mongoTemplate.count(query, UserFollow.class);
        response.setFansCount(fansCount);
        query = new Query();
        query.addCriteria(Criteria.where("infoCreatorId").is(id));
        long likeCount = mongoTemplate.count(query, ContentLike.class);
        response.setLikeCount(likeCount);
        query = new Query();
        query.addCriteria(Criteria.where("infoCreatorId").is(id));
        long shareCount = mongoTemplate.count(query, ContentShare.class);
        response.setShareCount(shareCount);

        return response;
    }

    @Override
    public MsgReadResponse userUnRead() {

        MsgReadResponse readResponse = new MsgReadResponse();

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("infoCreatorId").is(user.getId()));
        query.addCriteria(Criteria.where("read").is(false));
        long likeUnRead = mongoTemplate.count(query, ContentLike.class);
        readResponse.setLikeUnRead(likeUnRead);

        query = new Query();
        query.addCriteria(new Criteria().orOperator(Criteria.where("contentLinkId").is(user.getId()),
                Criteria.where("commentLinkId").is(user.getId())));
        query.addCriteria(Criteria.where("read").is(false));
        long commentUnRead = mongoTemplate.count(query, ContentComment.class);
        readResponse.setCommentUnRead(commentUnRead);

        return readResponse;
    }

    private void likeRead() {

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("infoCreatorId").is(user.getId()));
        query.addCriteria(Criteria.where("read").is(false));
        Update update = new Update();
        update.set("read", true);
        UpdateResult result = mongoTemplate.updateMulti(query, update, ContentLike.class);
        if (result.getModifiedCount() > 0) {
            log.info("点赞未读信息修改为已读,userId:{},count:{}", user.getId(), result.getModifiedCount());
        }

    }

    private void commentRead() {

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(new Criteria().orOperator(Criteria.where("contentLinkId").is(user.getId()),
                Criteria.where("commentLinkId").is(user.getId())));
        query.addCriteria(Criteria.where("read").is(false));
        Update update = new Update();
        update.set("read", true);
        UpdateResult result = mongoTemplate.updateMulti(query, update, ContentComment.class);
        if (result.getModifiedCount() > 0) {
            log.info("评论未读信息修改为已读,userId:{},count:{}", user.getId(), result.getModifiedCount());
        }

    }

    @Override
    public Page<MsgResponse> userLikeList(PageQueryRequest request) {

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("infoCreatorId").is(user.getId()));
        long count = mongoTemplate.count(query, ContentLike.class);
        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));

        List<ContentLike> contentLikeList = mongoTemplate.find(query.with(pageRequest), ContentLike.class);
        List<MsgResponse> list = contentLikeList.stream().map(contentLike -> {
            MsgResponse response = new MsgResponse();
            response.setContentId(contentLike.getInfoId());
            response.setContent(contentLike.getInfo());
            response.setType(contentLike.getType());
            if (ContentTypeEnum.PL.getCode().equals(contentLike.getType())) {
                //评论点赞返回评论关联的内容
                Query query1 = new Query();
                query1.addCriteria(Criteria.where("id").is(contentLike.getInfoId()));
                ContentComment comment = mongoTemplate.findOne(query1, ContentComment.class);
                if (comment != null) {
                    if(comment.getLevel().equals(1)){
                        //一级评论-返回评论信息关联的内容信息
                        CommentLinkResponse cl = new CommentLinkResponse();
                        cl.setContentType(comment.getContentType());
                        cl.setContentId(comment.getContentId());
                        response.setCommentLink(cl);
                    }else{
                        //二级评论查询一级评论关联的内容信息
                        query1 = new Query();
                        query1.addCriteria(Criteria.where("id").is(comment.getCommentId()));
                        ContentComment comment1 = mongoTemplate.findOne(query1, ContentComment.class);
                        if (comment1 != null) {
                            CommentLinkResponse cl = new CommentLinkResponse();
                            cl.setContentType(comment1.getContentType());
                            cl.setContentId(comment1.getContentId());
                            response.setCommentLink(cl);
                        }
                    }
                }
            }
            response.setMsgInfo("赞了你的" + this.setContentLikeType(contentLike.getType()));
            response.setRead(contentLike.isRead());
            response.setSenderId(contentLike.getCreatorId());
            response.setSenderNickName(contentLike.getCreatorNickName());
            response.setSenderAvatar(contentLike.getCreatorAvatar());
            response.setSendTime(contentLike.getCreatedTime());
            return response;
        }).collect(Collectors.toList());

        likeRead();

        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    private String setContentLikeType(String type) {
        if (StringUtils.isBlank(type)) {
            return "";
        }
        switch (type) {
            case "DK":
                return "打卡";
            case "DT":
                return "动态";
            case "PL":
                return "评论";
            case "WZ":
                return "文章";
            case "HT":
                return "话题";
            case "SP":
                return "商品";
            default:
                return "";

        }

    }

    @Override
    public Page<MsgResponse> userCommentList(PageQueryRequest request) {

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(new Criteria().orOperator(Criteria.where("contentLinkId").is(user.getId()),
                Criteria.where("commentLinkId").is(user.getId())));
        long count = mongoTemplate.count(query, ContentComment.class);
        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));

        List<ContentComment> commentList = mongoTemplate.find(query.with(pageRequest), ContentComment.class);
        List<MsgResponse> list = commentList.stream().map(comment -> {
            MsgResponse response = new MsgResponse();
            if (ContentTypeEnum.PL.getCode().equals(comment.getContentType())) {
                //二级评论返回评论关联的内容
                Query query1 = new Query();
                query1.addCriteria(Criteria.where("id").is(comment.getCommentId()));
                ContentComment comment1 = mongoTemplate.findOne(query1, ContentComment.class);
                if (comment1 != null) {
                    CommentLinkResponse cl = new CommentLinkResponse();
                    cl.setContentType(comment1.getContentType());
                    cl.setContentId(comment1.getContentId());
                    response.setCommentLink(cl);
                }
            }
            response.setContentId(comment.getContentId());
            response.setContent(comment.getInfo());
            response.setType(comment.getContentType());
            response.setMsgInfo(comment.getCommentContent());
            response.setRead(comment.isRead());
            response.setSenderId(comment.getCommentatorId());
            response.setSenderNickName(comment.getCommentatorNickName());
            response.setSenderAvatar(comment.getCommentatorAvatarUrl());
            response.setSendTime(comment.getCreatedTime());
            return response;
        }).collect(Collectors.toList());

        commentRead();

        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    @Override
    public boolean collect(UserCollectRequest request) {

        UserPO user = RequestContext.getCurUser();
        UserCollect collect = mapperFacade.map(request, UserCollect.class);
        collect.setCollectorId(user.getId());
        mongoTemplate.insert(collect);

        return true;
    }

    @Override
    public boolean isCollect(String infoId) {

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("infoId").is(infoId));
        query.addCriteria(Criteria.where("collectorId").is(user.getId()));

        return mongoTemplate.exists(query, UserCollect.class);
    }

    @Override
    public boolean unCollect(String infoId) {

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("infoId").is(infoId));
        query.addCriteria(Criteria.where("collectorId").is(user.getId()));
        DeleteResult result = mongoTemplate.remove(query, UserCollect.class);
        if (result.getDeletedCount() > 0) {
            log.info("取消收藏成功，userId:{},infoId:{}", user.getId(), infoId);
        }

        return true;
    }

    @Override
    public Page<UserCollectResponse> collectList(PageQueryRequest request) {

        UserPO user = RequestContext.getCurUser();
        Query query = new Query();
        query.addCriteria(Criteria.where("collectorId").is(user.getId()));
        long count = mongoTemplate.count(query, UserCollect.class);
        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<UserCollect> userCollectList = mongoTemplate.find(query.with(pageRequest), UserCollect.class);
        List<UserCollectResponse> list = userCollectList.stream().map(userCollect -> {
            UserCollectResponse response = mapperFacade.map(userCollect, UserCollectResponse.class);
            return response;
        }).collect(Collectors.toList());

        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    @Override
    public UserStatResponse userStatistics() {
        UserPO user = RequestContext.getCurUser();
        String id = user.getId();

        UserStatResponse response = new UserStatResponse();

        Query query = new Query();
        query.addCriteria(Criteria.where("followerId").is(id));
        long followCount = mongoTemplate.count(query, UserFollow.class);
        response.setFollowCount(followCount);
        query = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        long fansCount = mongoTemplate.count(query, UserFollow.class);
        response.setFansCount(fansCount);
        query = new Query();
        query.addCriteria(Criteria.where("collectorId").is(id));
        long collectCount = mongoTemplate.count(query, UserCollect.class);
        response.setCollectCount(collectCount);
        query = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        long certificateCount = mongoTemplate.count(query, CertificatePO.class);
        response.setCertificateCount(certificateCount);

        return response;
    }

    @Override
    public Long queryCommentCount(String contentId, String type) {
        Query query = new Query();
        query.addCriteria(Criteria.where("contentId").is(contentId));
        query.addCriteria(Criteria.where("contentType").is(type));
        long count = mongoTemplate.count(query, ContentComment.class);
        return count;
    }

    @Override
    public Long queryLikeCount(String contentId, String type) {
        Query query = new Query();
        query.addCriteria(Criteria.where("infoId").is(contentId));
        query.addCriteria(Criteria.where("type").is(type));
        long count = mongoTemplate.count(query, ContentLike.class);
        return count;
    }

    @Override
    public Long queryShareCount(String contentId, String type) {
        Query query = new Query();
        query.addCriteria(Criteria.where("infoId").is(contentId));
        query.addCriteria(Criteria.where("type").is(type));
        long count = mongoTemplate.count(query, ContentShare.class);
        return count;
    }


    @Override
    public boolean testMq() {
        InteractiveMqRequest request = new InteractiveMqRequest();
        request.setType(ContentTypeEnum.DT.getCode());
        request.setAction(InteractiveActionEnum.COMMENT.getCode());
        request.setBid("123456");
//        mqService.sendNormal(request, CampusMQDefinition.ROUTING_KEY_INTERACTIVE);
        return true;
    }
}
